package cn.itcast.work.dao.commit;

import cn.itcast.work.pojo.Commit;
import cn.itcast.work.pojo.Recover;
import cn.itcast.work.pojo.Text;
import cn.itcast.work.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * * @author: Mao Wanting
 * * @date: 2024/6/10
 * *description:评论和回复的加载
 **/

@Mapper
public interface CommitSearch {
    /**
     * 新增回复（需要给username,text_id,commit--content,time）
     *
     * @param user
     * @param text
     * @param commit
     * @return
     */
    @Insert("insert into commit(id,user_id,text_id,content,time) " +
            "values(" +
            "null," +
            "#{user.id}," +
            "#{text.id}," +
            "#{commit.content}," +
            "#{commit.time})")
    public int addCommit(@Param("user") User user,
                         @Param("text") Text text,
                         @Param("commit") Commit commit);

    /**
     * 删除评论（给username,commit.time）
     *
     * @param user
     * @param commit
     * @return
     */
    @Delete("delete commit " +
            "from users,commit " +
            "where users.userName=#{user.username} " +
            "and commit.time=#{commit.timeString} " +
            "and users.id=commit.user_id")
    public int deleteCommit(@Param("user") User user,
                            @Param("commit") Commit commit);

    /**
     * 新增回复（给username，recover的content，time）
     *
     * @param user
     * @param commit
     * @param recover
     * @return
     */
    @Insert("insert into recover(id,user_id,commit_id,content,time) " +
            "values(" +
            "null," +
            "#{user.id}," +
            "(select id from commit where commit.time=#{commit.timeString})," +
            "#{recover.content}," +
            "#{recover.time})")
    public int addRecover(@Param("user") User user,
                          @Param("commit") Commit commit,
                          @Param("recover") Recover recover);

    /**
     * 删除回复（给username，recover的content和time）
     *
     * @param user
     * @param commit
     * @param recover
     * @return
     */
    @Delete("delete recover " +
            "from commit,users,recover " +
            "where commit.time=#{commit.timeString}" +
            "and recover.content=#{recover.content} " +
            "and recover.time=#{recover.timeString}" +
            "and recover.commit_id=commit.id")
    public int deleteRecover(@Param("user") User user,
                             @Param("commit") Commit commit,
                             @Param("recover") Recover recover);



    /**
     * 输出评论（text_id）
     * 通过文章的id输出所有的评论信息
     *
     * @param text_id
     * @return
     */
    @Select("select * from commit where text_id=#{text_id}")
    List<Commit> selectAllCommit(int text_id);

    //通过commit的user_id查出user信息
    @Select("select * from users " +
            "join commit on commit.user_id=users.id " +
            "where commit.id=#{id} and commit.user_id=#{userid} and commit.content=#{content}")
    public User selectUserByCommit(Commit commit);

    //通过recover的user_id查出user信息
    @Select("select * from users " +
            "join recover on recover.user_id=users.id " +
            "join commit on recover.commit_id=commit.id " +
            "where recover.user_id=#{userid} " +
            "and recover.id=#{id} " +
            "and recover.content=#{content} " +
            "and recover.commit_id=#{commitid}")
    public User selectUserByRecover(Recover recover);

    /**
     * 输出回复（给commit的content和time）
     *
     * @param commit
     * @return
     */
    @Select("select recover.id,recover.user_id,recover.commit_id,recover.content,recover.time " +
            "from recover,commit " +
            "where commit.time=#{timeString} " +
            "and commit.content=#{content} " +
            "and commit.id=recover.commit_id")
    public List<Recover> selectAllRecover(Commit commit);


    //通过text的title查id
    @Select("select id from text where title=#{title}")
    public int selectTextIdByTitle(Text text);


    //删除回复：强制删除
    @Delete("delete recover " +
            "from commit,recover " +
            "where commit.time=#{commit.timeString}" +
            "and recover.content=#{recover.content} " +
            "and recover.time=#{recover.timeString}" +
            "and recover.commit_id=commit.id")
    public int DeleteRecover(@Param("recover") Recover recover,
                             @Param("commit") Commit commit);

    //删除评论
    @Delete("delete commit from commit where content=#{content} and time=#{timeString} and id=#{id}")
    public int DeleteCommit(Commit commit);

}
